from model.job import Job
from sqlalchemy import desc, or_
from model.user_deliver_status import UserDeliverStatus
from model.province_city import ProvinceCity


class JobDao(object):
    def __init__(self, session):
        self.session = session

    def add(self, job):
        self.session.add(job)

    def merge(self, job):
        self.session.merge(job)

    def get_by_id(self, job_id):
        return self.session.query(Job).filter(Job.job_id == job_id).first()

    def get_user_send_dict(self, user_id):
        job_set = set()
        q = self.session.query(UserDeliverStatus,
                               UserDeliverStatus.user_id, UserDeliverStatus.job_id) \
            .filter(UserDeliverStatus.user_id == user_id).all()
        if q:
            for d in q:
                job_set.add(str(d.job_id))
            return job_set

    @staticmethod
    def get_province_dict(session):
        p_dict = {}
        q = session.query(ProvinceCity).all()
        if q:
            for d in q:
                p_dict[str(d.code)] = d.name
            return p_dict

    def get_list(self, province_code=None,
                 city_code=None,
                 key=None,
                 job_type=3,
                 job_class=None,
                 job_post=None,
                 country_code=None,
                 page=0, limit=0, user_id=None):
        p_dict = JobDao.get_province_dict(self.session)
        job_set = self.get_user_send_dict(user_id=user_id)
        try:
            ##不包含暂停的职位
            q = self.session.query(Job).filter(Job.job_status != 3)
            if province_code:
                q = q.filter(Job.province_code == province_code)
            if city_code:
                q = q.filter(Job.city_code == city_code)
            if job_class:
                q = q.filter(Job.job_class == job_class)
            if country_code:
                # 只传递区号
                if not province_code and not city_code:
                    c_code = country_code[0:4]
                    c_code = c_code + "00"
                    q = q.filter(or_(Job.country_code == country_code, Job.city_code == c_code))
                else:
                    q = q.filter(Job.country_code == country_code)
            if job_post:
                q = q.filter(Job.job_post == job_post)
            if job_type:
                q = q.filter(Job.job_type == job_type)
            if key:
                q = q.filter(
                    or_(Job.job_name.like('%' + key + '%'),
                        Job.job_content.like('%' + key + '%'))
                )

            total = q.count()
            if limit > 0:
                q = q.order_by(desc(Job.job_id)).limit(limit).offset(page * limit)

            max_page_num, div = divmod(total, limit)
            if div:
                max_page_num += 1
            result = q.all()
            f_list = []
            if result:
                for row in result:
                    m_job_id = str(row.job_id)
                    ##是否投递过简历
                    has_job = 0
                    if job_set and m_job_id in job_set:
                        has_job = 1

                    f_list.append(
                        {
                            'has_job': has_job,
                            'job_id': m_job_id,
                            'job_name': row.job_name,
                            'salary_code': row.salary_code,
                            'part_salary': row.part_salary,
                            'job_status': row.job_status,
                            'job_time': row.job_time,
                            'job_type': row.job_type,
                            'job_class': row.job_class,
                            'job_place': row.job_place,
                            'job_count': row.job_count,
                            'job_content': row.job_content,
                            'job_requirement': row.job_requirement,
                            'job_remark': row.job_remark,
                            'job_cycle': row.job_cycle if row.job_cycle else '',
                            'company_id': row.company_id,
                            'province_code': row.province_code,
                            'city_code': row.city_code,
                            'balance_type': row.balance_type,
                            'country_"code': row.country_code,
                            'edu_code': row.edu_code,
                            "exp_req": row.exp_req,
                            'province_name': p_dict.get(row.province_code, ''),
                            'city_name': p_dict.get(row.city_code, ''),
                            'welfare': row.welfare if row.welfare else '',
                            'age_req': row.age_req if row.age_req else '',
                            'sex_req': row.sex_req if row.sex_req else '',
                            'attach_req': row.attach_req if row.attach_req else ''
                        }
                    )

            return {'list': f_list,
                    'total': total,
                    "curpage": page,
                    "total_page": max_page_num}
        except Exception as ex:
            raise ex
            return None


if __name__ == '__main__':
    pass
